//
//  MYShopGoodsCommentVC.m
//  MuYing
//
//  Created by tbb on 2020/2/22.
//  Copyright © 2020 TBB. All rights reserved.
//

#import "MYShopGoodsCommentVC.h"
#import "MYDynamicCommentCell.h"
#import "YQPCommentListModel.h"
@interface MYShopGoodsCommentVC ()

@end

@implementation MYShopGoodsCommentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = kYQLS(@"商品评价");
    kYQREGISTERTABLEVIEWNIBCELL(@"MYShopDetailsCommentCell")
    [self setupRefresh:self.listTableView];
    
    [self onStartRefresh];
}

- (void)onStartRefresh {
    self.page = 1;
    [self getGoodsCommentData];
}

-(void)onStartLoadMore {
    self.page ++;
    [self getGoodsCommentData];
}

-(void)getGoodsCommentData {
    NSMutableDictionary *par = [NSMutableDictionary dictionary];
    par[@"id"] = self.goodsID;
    par[@"size"] = @"10";
    par[@"pageNo"] = @(self.page);
    par[@"type"] = @"0";
    YQSHOWLOADINGINVIEW
    [YQPNetWorkingManager sendPostRequest:kPOST_QUERY_GOOD_COMMENT_LIST_URL parameters:par success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
        if (responseObject.code == 0) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
                NSArray *arr = [YQPSpuCommentListModel mj_objectArrayWithKeyValuesArray:responseObject.data[@"list"]];
                [self.dataArr addObjectsFromArray:arr];
                [self.listTableView reloadData];
                if (arr.count == 0) {
                    [self.listTableView.mj_footer endRefreshingWithNoMoreData];
                }
            }
        }else{
            YQUITIPSERROR(responseObject.msg);
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [self stopRefresh];
        YQHIDELOADINGTIPS
        YQUITIPSERROR(error);
    }];
}



- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YQPSpuCommentListModel *commetnModel = nil;
    if (indexPath.row < self.dataArr.count) {
        commetnModel = self.dataArr[indexPath.row];
    }
    MYShopDetailsCommentCell *commentCell = [tableView dequeueReusableCellWithIdentifier:@"MYShopDetailsCommentCell"];
    commentCell.userNameLb.text = commetnModel.userNikeName.length > 0 ?commetnModel.userNikeName:kYQLS(@"匿名");
     commentCell.timeLb.text = [YQPInsureValidate timeInStr:commetnModel.createTime];
     commentCell.colorLb.text = commetnModel.productAttribute;
     commentCell.contentLb.text = commetnModel.content;
     [commentCell.userHeaerIcon sd_setImageWithURL:[NSURL URLWithString:commetnModel.userIcon] placeholderImage:IMAGE_NAMED(@"")];
    return commentCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    YQPSpuCommentListModel *model = nil;
    if (indexPath.row < self.dataArr.count) {
        model = self.dataArr[indexPath.row];
    }
    
    return [tableView fd_heightForCellWithIdentifier:@"MYShopDetailsCommentCell" configuration:^(MYShopDetailsCommentCell *commentCell) {
        commentCell.userNameLb.text = model.userNikeName.length > 0 ?model.userNikeName:kYQLS(@"匿名");
          commentCell.timeLb.text = [YQPInsureValidate timeInStr:model.createTime];
          commentCell.colorLb.text = model.productAttribute;//[NSString stringWithFormat:@"%@: %@",kYQLS(@"颜色分类"),@"红色"];//@": 红色";
          commentCell.contentLb.text = model.content;
          [commentCell.userHeaerIcon sd_setImageWithURL:[NSURL URLWithString:model.userIcon] placeholderImage:IMAGE_NAMED(@"")];
    }];
    
}

@end
